home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V1 CW9 / 68K and PPC Projects (CW9) / 7.03 - ShowClip / ShowClip.c < prev    next >
C/C++ Source or Header  |  1996-06-01  |  3KB  |  131 lines

  1. /********************************************************/
  2. /*                                                        */
  3. /*  ShowClip Code from Chapter Seven of                 */
  4. /*                                                        */
  5. /*    ** The Macintosh C Programming Primer, 2nd Ed. **    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /********************************************************/
  10.  
  11. #define kBaseResID            128
  12. #define    kMoveToFront        (WindowPtr)-1L
  13.  
  14. #define    kEmptyString        "\p"
  15. #define kNilFilterProc        nil
  16.  
  17. #define    kErrorAlertID        kBaseResID
  18.  
  19.  
  20. /***************/
  21. /*  Functions  */
  22. /***************/
  23.  
  24. void    ToolBoxInit( void );
  25. void    WindowInit( void );
  26. void    MainLoop( void );
  27. void    CenterPict( PicHandle picture, Rect *destRectPtr );
  28. void    DoError( Str255 errorString, Boolean fatal );
  29.  
  30.  
  31. /****************** main ***************************/
  32.  
  33. void    main( void )
  34. {
  35.     ToolBoxInit();
  36.     WindowInit();
  37.     MainLoop();
  38. }
  39.  
  40.  
  41. /****************** ToolBoxInit *********************/
  42.  
  43. void    ToolBoxInit( void )
  44. {
  45.     InitGraf( &qd.thePort );
  46.     InitFonts();
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs( 0L );
  51.     InitCursor();
  52. }
  53.  
  54.  
  55. /****************** WindowInit ***********************/
  56.  
  57. void    WindowInit( void )
  58. {
  59.     WindowPtr    window;
  60.     
  61.     window = GetNewWindow( kBaseResID, nil, kMoveToFront );
  62.     
  63.     if ( window == nil )
  64.         DoError( "\pCan't load the WIND resource!", true );
  65.         
  66.     ShowWindow( window );
  67.     SetPort( window );
  68. }
  69.  
  70.  
  71. /******************************** MainLoop *********/
  72.  
  73. void    MainLoop( void )
  74. {
  75.     Rect        pictureRect;
  76.     Handle        clipHandle;
  77.     long        length, offset;
  78.     WindowPtr    window;
  79.     
  80.     clipHandle = NewHandle( 0 );
  81.     window = FrontWindow();
  82.             
  83.     if ( ( length = GetScrap( clipHandle, 'TEXT', &offset ) ) < 0 )
  84.     {
  85.         if ( GetScrap( clipHandle, 'PICT', &offset ) < 0 )
  86.             DoError( "\pThere's no PICT and no text in the scrap...", true );
  87.         else
  88.         {
  89.             pictureRect = window->portRect;
  90.             CenterPict( (PicHandle)clipHandle, &pictureRect );
  91.             DrawPicture( (PicHandle)clipHandle, &pictureRect );
  92.         }
  93.     }
  94.     else
  95.     {
  96.         HLock( clipHandle );
  97.         TextBox( *clipHandle, length, &(window->portRect), teJustLeft );
  98.         HUnlock( clipHandle );
  99.     }
  100.  
  101.     while ( !Button() ) ;
  102. }
  103.  
  104.  
  105. /****************** CenterPict ********************/
  106.  
  107. void    CenterPict( PicHandle picture, Rect *destRectPtr )
  108. {
  109.     Rect    windRect, pictRect;
  110.     
  111.     windRect = *destRectPtr;
  112.     pictRect = (**( picture )).picFrame;
  113.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  114.                            windRect.top     - pictRect.top);
  115.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  116.                           (windRect.bottom - pictRect.bottom)/2);
  117.     *destRectPtr = pictRect;
  118. }
  119.  
  120.  
  121. /***************** DoError ********************/
  122.  
  123. void    DoError( Str255 errorString, Boolean fatal )
  124. {
  125.     ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
  126.     
  127.     StopAlert( kErrorAlertID, kNilFilterProc );
  128.     
  129.     if ( fatal )
  130.         ExitToShell();
  131. }